package com.hutiao.snowhb.actions.wechat;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.hutiao.snowhb.wechat.tools.WeiXinTools;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/wechat")
public class WechatCheckAction extends ActionSupport {
	
	private static Logger logger = Logger.getLogger(WechatCheckAction.class);

	private static final long serialVersionUID = 2043965774129440240L;
	
	private static final String TOKEN = "2043965774129440240L";
	
	private String signature;
	
	private String timestamp;
	
	private String nonce;
	
	private String echostr;
	
	private String result = "";

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getNonce() {
		return nonce;
	}

	public void setNonce(String nonce) {
		this.nonce = nonce;
	}

	public String getEchostr() {
		return echostr;
	}

	public void setEchostr(String echostr) {
		this.echostr = echostr;
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Action(value="check",results = {
			@Result(name="success", type="string", params={"stringName", "result"})})
	public String execute() throws Exception {
		
		logger.info("timestamp:" + timestamp);
		logger.info("nonce:" + nonce);
		logger.info("echostr:" + echostr);
		logger.info("signature:" + signature);
		
		if (null == timestamp && null == nonce && null == echostr && null == signature) {
			this.result = "param error";
			return SUCCESS;
		}
		
		if (WeiXinTools.access(TOKEN, signature, timestamp, nonce)) {
			result = echostr;
		} else {
			result = "验证错误";
		}
	
		return SUCCESS;
	}

}
